package com.hzqy.service.wailian;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.commons.utils.SystemConfigUtils;
import com.hzqy.persistence.dao.picture.PictureDao;
import com.hzqy.persistence.dao.wailian.WaiLianDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.service.pms.PmsServiceBean;
import com.hzqy.service.system.SysConfigServiceBeen;
import com.hzqy.web.vo.PmsUserVo;
import com.hzqy.web.vo.system.SysConfigVo;
import com.hzqy.web.vo.WlContentVo;

@Service
public class WaiLianServiceBean  extends GenericServiceBean<WlContentVo, WaiLianDao, Integer> {

	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private WaiLianDao waiLianDao;
	@Resource
	private PictureDao pictureDao;
	@Resource
	private SysConfigServiceBeen sysConfigServiceBeen;
	
	@Resource
	private PmsServiceBean pmsServiceBean;
	
	@Override
	public WaiLianDao getEntityDao() {
		return waiLianDao;
	}
	public  Map<String,Object> insertWaiLian(WlContentVo wlvo) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag =waiLianDao.insertWaiLian(wlvo);
		if(flag>0){
			resultMap.put("success", true);
			resultMap.put("result","新增内容成功!");
		}else{
			resultMap.put("success", false);
			resultMap.put("result","新增内容失败!");
		}
		return resultMap;
	}
	
	public Map<String, Object> selectWaiLian(PmsUserVo userVo,WlContentVo wlvo, int curPage, int pageSize) {
		Map<String, Object> map = new HashMap<>();
		
		if(userVo.getParentOrgId() > 0){
			String orgIds = pmsServiceBean.getAuthorityPmsOrgIds(userVo);
			if(wlvo.getF_wlc_orgname()==null||"".equals(wlvo.getF_wlc_orgname())||wlvo.getF_wlc_orgname().equals(orgIds)){
				wlvo.setOrgIds(orgIds);
			}else{
				boolean flag=true;
				String[] orgIdlist=orgIds.split(",");
				for(int i=0;i<orgIdlist.length;i++){
					if(wlvo.getF_wlc_orgname().equals(orgIdlist[i])){
						flag=false;
						break;
					}
				}
				if(flag){//该企业不在列表内则不可查询
					wlvo.setOrgIds(orgIds);
				}
				
			}
		}
		
		if("0".equals(wlvo.getOrgIds())) {
			wlvo.setOrgIds("");
		}
		
		int rows = waiLianDao.selectWaiLianCount(wlvo);
		map.put("total", rows);
		map.put("f_wlc_path", wlvo.getF_wlc_path());
		map.put("f_wlc_orgname", wlvo.getF_wlc_orgname());
		map.put("f_wlc_type", wlvo.getF_wlc_type());
		map.put("f_wlc_control", wlvo.getF_wlc_control());
		map.put("f_wlc_name", wlvo.getF_wlc_name());
		map.put("orgIds", wlvo.getOrgIds());
		map.put("firstResult", (curPage-1)*pageSize);
		map.put("maxResults", pageSize);
		
		List<WlContentVo> results = waiLianDao.selectWaiLian(map);
		map.put("rows", results);
		return map;
	}
	public Map<String, Object> deleteWailianContent(int f_wlc_id) {
		Map<String, Object> resultMap = new HashMap<>();
		int flag = waiLianDao.deleteWailianContent(f_wlc_id);
		if(flag>0){
			resultMap.put("success", true);
			resultMap.put("result","删除内容成功!");
		}else{
			resultMap.put("success", false);
			resultMap.put("result","删除内容失败!");
		}
		return resultMap;
	}
	public WlContentVo selectWaiLianById(int f_wlc_id) {
		WlContentVo contentVo= waiLianDao.selectWaiLianById(f_wlc_id);
		return contentVo;
	}
	public Map<String, Object> updateWailian(WlContentVo wlContentVo) {
		Map<String, Object> resultMap = new HashMap<>();
		int flag = waiLianDao.updateWaiLianById(wlContentVo);
		if(flag>0){
			resultMap.put("success", true);
			resultMap.put("result","更新内容成功!");
		}else{
			resultMap.put("success", false);
			resultMap.put("result","更新内容失败!");
		}
		return resultMap;
	}
	public int reviewWlContentVo(WlContentVo wlContentVo, PmsUserVo userVo) {
		int flag = 0;
		if(wlContentVo==null) {
			flag = -1;
		} else if (wlContentVo.getF_wlc_status() !=1 && wlContentVo.getF_wlc_status()!=4 
				&& wlContentVo.getF_wlc_status() != 13
				&& wlContentVo.getF_wlc_status() != 14
				&& wlContentVo.getF_wlc_status() != 15
				&& wlContentVo.getF_wlc_status() != 16) {
			flag = -4;
		}
		if(flag == 0){
			String roleId=userVo.getRoleIds();
			if(roleId!=null&&!"".equals(roleId)){//验证角色如果为省或者超级管理员则不改变内容状态
				if("1".equals(userVo.getRoleIds())){//超级管理员
					 //不做判断，随便改
				}else if("2".equals(userVo.getRoleIds())||(SystemConfigUtils.getSysConfig("review_province")+"").equals(userVo.getRoleIds())){//省管理员
					if(wlContentVo.getF_wlc_status()==1||wlContentVo.getF_wlc_status()==4){
						//职则内操作
					}else{
						flag = -3;
					}
				}else{
					flag = -3;
				}
			}else {
				flag = -3;
			}
			if(flag!=0){
				return flag;
			}
			waiLianDao.reviewWaiLian(wlContentVo);
			return 1;
		}
		return flag;
	}
	
	public SysConfigVo selectSysConfig(SysConfigVo sysConfigVo) {
		SysConfigVo sc = waiLianDao.selectSysConfig(sysConfigVo);
		return sc;
	} 
	
	public WlContentVo selectWLContent(WlContentVo wlContentVo) {
		WlContentVo wlc = waiLianDao.selectWLContent(wlContentVo);
		return wlc;
	} 
	
	public WlContentVo selectWLContentAvaliable(WlContentVo wlContentVo) {
		LOG.debug("查询外链id："+wlContentVo.getF_wlc_id());
		WlContentVo wlc = waiLianDao.selectWLContent(wlContentVo);
		
		if(wlc!=null){
			SysConfigVo sysConfigVo = new SysConfigVo();
			sysConfigVo.setF_sc_key(wlContentVo.getSysConfigKey());
			wlc.setSysConfigVo(this.selectSysConfig(sysConfigVo));
			
			if(wlc.getF_wlc_status() == 1) {//外链必须为上线状态才可用
				if(wlc.getF_wlc_control() == 1) {//不受管控的可以直接使用
					return wlc;
				} else if(wlc.getF_wlc_control() == 0) {//受到管控 需要判断屏蔽开关是否开启
					if("1".equals(wlc.getSysConfigVo().getF_sc_value().trim())) {//开关为关闭状态，允许外链访问
						return wlc;
					}
				}
			}
		}
		
		return null;
	} 
	
	public int batchUnReviewContent(String ids) {
		int flag = 0;
		String[] idsArr = ids.split(",");
		for(int i=0;i<idsArr.length;i++) {
			WlContentVo wlContentVo = new WlContentVo();
			wlContentVo.setF_wlc_id(Integer.parseInt(idsArr[i]));
			wlContentVo.setF_wlc_status(2);
			wlContentVo.setF_wlc_remark("");
			flag += waiLianDao.reviewWaiLian(wlContentVo);
		}
		return flag;
	}
	public int batchDeleteContent(String ids) {
		int flag = 0;
		String[] idsArr = ids.split(",");
		for(int i=0;i<idsArr.length;i++) {
			flag += waiLianDao.deleteWailianContent(Integer.valueOf(idsArr[i]));
		}
		return flag;
	}
}
